/**
 * @param {number[]} nums
 * @return {number[][]}
 */
var permute = function (nums) {
  const length = nums.length
  let res = []

  function backtrack(cur, target) {
    if (cur.length == length) {
      res.push(cur)
    }

    for (let i = 0; i < target.length; i++) {
      const temp = [...target]
      temp.splice(i, 1)
      backtrack(cur.concat(target[i]), temp)
    }
  }

  backtrack([], nums)

  return res
}

console.log(permute([1, 2, 3]))
